//
//  NewSetVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/3/26.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "NewSetVC.h"
#import "setCell.h"
#import "UpdateVC.h"

@interface NewSetVC ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *left_table;
@property (strong, nonatomic) IBOutlet UITableView *bottom_table;
@property (strong, nonatomic) IBOutlet UITableView *right_table;
@property (strong, nonatomic) IBOutlet UILabel *left_lab;
@property (strong, nonatomic) IBOutlet UILabel *right_lab;
@property (strong, nonatomic) IBOutlet UILabel *bottom_lab;
@property (strong, nonatomic) IBOutlet UIButton *delete_btn;
@property (strong, nonatomic) IBOutlet UIButton *quite_btn;

@end

@implementation NewSetVC{
    NSMutableArray *titleArr;
    NSMutableDictionary *liveDic;//播放
    NSMutableDictionary *wifiDic;//
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self.backKuang setHidden:YES];
    [self showBackCenterKuang:[UIImage imageNamed:@"back_center1"]];
    self.left_lab.font = self.right_lab.font = self.bottom_lab.font = self.delete_btn.titleLabel.font = self.quite_btn.titleLabel.font =AutoLabFont2;
    
    [self.delete_btn addTarget:self action:@selector(deleteAccount) forControlEvents:UIControlEventTouchUpInside];
    [self.quite_btn addTarget:self action:@selector(quiteAccount) forControlEvents:UIControlEventTouchUpInside];
    liveDic = [NSMutableDictionary dictionary];
    wifiDic = [NSMutableDictionary dictionary];
    titleArr = [NSMutableArray arrayWithObjects:@[@"播放时自动跳过片尾",@"允许4G/5G网络播放"],@[@"消息通知",@"清除缓存"],@[@"版本更新",@"评价",@"联系客服"], nil];
    
    self.left_table.delegate = self.right_table.delegate = self.bottom_table.delegate = self;
    self.left_table.dataSource = self.right_table.dataSource = self.bottom_table.dataSource = self;
    [self.left_table registerNib:[UINib nibWithNibName:@"setCell" bundle:nil] forCellReuseIdentifier:@"setCell"];
    [self.right_table registerNib:[UINib nibWithNibName:@"setCell" bundle:nil] forCellReuseIdentifier:@"setCell"];
    [self.bottom_table registerNib:[UINib nibWithNibName:@"setCell" bundle:nil] forCellReuseIdentifier:@"setCell"];
    self.left_table.tableFooterView = self.right_table.tableFooterView = self.bottom_table.tableFooterView = [UIView new];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.left_table) {
        return [titleArr[0]count];
    }else if (tableView == self.right_table) {
        return [titleArr[1]count];
    }else{
        return [titleArr[2]count];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return self.left_table.frame.size.height/[titleArr[0]count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    setCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView == self.left_table) {
        cell.name_lab.text = [NSString stringWithFormat:@"%@",titleArr[0][indexPath.row]];
//        [cell.icon_btn setBackgroundImage:[UIImage imageNamed:@"turn_on"] forState:UIControlStateNormal];
//        cell.icon_btn.tag = 1000+indexPath.section+indexPath.row;
        [cell.icon_btn addTarget:self action:@selector(turnOn_Off_Btn:) forControlEvents:UIControlEventTouchUpInside];
        if (indexPath.row == 0) {
            cell.icon_btn.tag = 10000;
            if ([liveDic[@"liveTurnOff"] CheckNSStringData]) {
                [cell.icon_btn setBackgroundImage:[UIImage imageNamed:@"turn_off"] forState:UIControlStateNormal];
            }else{
                [cell.icon_btn setBackgroundImage:[UIImage imageNamed:@"turn_on"] forState:UIControlStateNormal];
            }
        }else{
            cell.icon_btn.tag = 10001;
            if ([wifiDic[@"wifiTurnOff"] CheckNSStringData]) {
                [cell.icon_btn setBackgroundImage:[UIImage imageNamed:@"turn_off"] forState:UIControlStateNormal];
            }else{
                [cell.icon_btn setBackgroundImage:[UIImage imageNamed:@"turn_on"] forState:UIControlStateNormal];
            }
        }

        cell.content_lab.text = @"";
        
    }else if (tableView == self.right_table) {
        cell.name_lab.text = [NSString stringWithFormat:@"%@",titleArr[1][indexPath.row]];
        [cell.icon_btn setBackgroundImage:[UIImage imageNamed:@"mine_arrow"] forState:UIControlStateNormal];
        if (indexPath.row == 1) {
            cell.content_lab.text = [NSString stringWithFormat:@"%.2fMB",[DataService calculateCache]];
        }else{
           cell.content_lab.text = @"";
        }
    }else{
        cell.name_lab.text = [NSString stringWithFormat:@"%@",titleArr[2][indexPath.row]];
        [cell.icon_btn setBackgroundImage:[UIImage imageNamed:@"mine_arrow"] forState:UIControlStateNormal];
        if (indexPath.row == 0) {
            //当前版本号
            cell.content_lab.text = @"1.0";
        }else if (indexPath.row == 2){
            //联系客服
            cell.content_lab.text = @"422-100-980";
        }else{
            cell.content_lab.text = @"";
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     if (tableView == self.left_table) {
         if (indexPath.row == 0) {
             //跳过片头片尾
             
             
         }else{
             //4g播放
             
         }
         
       }else if (tableView == self.right_table) {
           if (indexPath.row == 0) {
               //消息通知
               UIAlertController *alterVC = [UIAlertController alertControllerWithTitle:@"您确定要跳转到系统设置推送吗？" message:nil preferredStyle:UIAlertControllerStyleAlert];
               [alterVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
               [alterVC addAction:[UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                   //直接跳转到app所对应的系统设置，开启对应权限
                   NSURL * appSettingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                   if ([[UIApplication sharedApplication] canOpenURL:appSettingURL]){
                       [[UIApplication sharedApplication] openURL:appSettingURL options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {
                           
                       }];
                   }
               }]];
               [self presentViewController:alterVC animated:YES completion:nil];
               
           }else{
               //清除缓存
               
           }
           
       }else{
           if (indexPath.row == 0) {
               //版本更新
               UpdateVC *vc = [[UpdateVC alloc]init];
               [self PushToViewController:vc];
               
           }else if (indexPath.row == 1) {
               //评价
               [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://:apps.apple.com/gb/app/yi-dong-cai-bian/id391945719"]];
               
           }else{
               //联系客服
               
           }
           
       }
}


/*
 *  注销
 */
-(void)deleteAccount{
    
    
}

/*
 * 退出登录
 */
-(void)quiteAccount{
    NSUDRemoveValueWithKey(@"Token");
    AppDelegate *appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    LoginVC *vc = [[LoginVC alloc]init];
    BaseNavVC * nav = [[BaseNavVC alloc]initWithRootViewController:vc];
    appDelegate.window.rootViewController = nav;
    appDelegate.nav = nav;
    
}

/*
 * 播放设置
 */
-(void)turnOn_Off_Btn:(UIButton *)sender{
    if (sender.tag == 10000) {
        if ([liveDic[@"liveTurnOff"] CheckNSStringData]) {
            [liveDic removeObjectForKey:@"liveTurnOff"];
            [self ShowNewMsg:@"1开"];
        }else{
            [liveDic setValue:@"1" forKey:@"liveTurnOff"];
            [self ShowNewMsg:@"1关"];
        }
    }else{
        if ([wifiDic[@"wifiTurnOff"] CheckNSStringData]) {
            [wifiDic removeObjectForKey:@"wifiTurnOff"];
            [self ShowNewMsg:@"2开"];

        }else{
            [wifiDic setValue:@"1" forKey:@"wifiTurnOff"];
            [self ShowNewMsg:@"2关"];
        }
    }
    [_left_table reloadData];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
